<template>
	<view class="imagePage">
		
		<!-- 推荐好友 -->
		<view class="recommend" v-if="type == 1">
			<image class="codeTwo" :src="pageData" mode=""></image>
			<!-- src="/static/my/dz.png" -->
			<image :src="image+'assets/web_img/p2.png'" mode=""></image>
			<button class="invitation btn" @tap.stop="shareClick" open-type="share" >发送好友</button>
			<view class="generate btn" @tap.stop="downloadImage">生成图片</view>
		</view>
		
		<!-- pk胜 -->
		<view class="PKWin" v-if="type == 2">
			<image :src="image+'assets/web_img/edit1.png'" mode=""></image></image>
			<view class="btn" v-if="isType != 2"  @tap.stop="goShop">去点餐</view>
		</view>
		
		<!-- pk负 -->
		<view class="PKNegative" v-if="type == 3">
			<image :src="image+'assets/web_img/edit17.png'" mode=""></image>
			<view class="btn" v-if="isType != 2" @tap.stop="comeAgain">再来一次</view>
		</view>
		
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				image: this.$base.smallimgUrl,
				type: 1, // type  1代表是 推荐好友  2代表是 pk胜  3代表是  pk负
				pageData: '',
				userInfo: uni.getStorageSync('userInfo'),
				share: {},
				isType:0,
				img_hb:''
			};
		},
		onLoad(el) {
			this.type = el.type
			this.isType = el.isType
			if(this.type == 1){
				this.getPageData()
			}
		},
		onShareAppMessage() {
			return {
				title: this.share.title,
				path: this.share.path,
				imageUrl: this.share.imageUrl,
				desc: this.share.desc,
				content: this.share.content,
				success(res) {
					this.$utils.toast('分享成功')
					this.share = {}
					console.log(res)
				},
				fail(res) {
					this.$utils.toast('分享失败')
				}
			}
		},
		methods: {
			// 推荐好友
			getPageData(){
				let that = this
				that.$api.inviteFriends().then(res => {
					that.pageData = res.data.data.img
					// console.log(that.pageData,'that.pageData')
					that.img_hb = res.data.data.img_hb
				})
			},
			// 点击下载图片
			downloadImage(){
				let that = this
				console.log(that.img_hb,'that.pageData')
				uni.downloadFile({
					url: that.img_hb,
					success(res) {
						let tempFilePath = res.tempFilePath
						uni.saveImageToPhotosAlbum({
							filePath: tempFilePath,
							success(res1){
								that.$utils.toast('图片生成成功')
							},
							fail(err){
								that.$utils.toast('图片生成失败')
							}
						})
					}
				})
			},
			// 点击确定，发给好友
			shareClick(){
				console.log(this.userInfo.id,'this.userInfo.id')
				this.share = {
					title: '',
					path: `/pages/tabBar/home/home?inviter_id=${this.userInfo.id}`,
					imageUrl: '',
					desc: '',
					content: ''
				}
			},
			
			// 点击跳转点餐
			goShop(){
				uni.switchTab({
					url: '/pages/tabBar/shop/shop'
				})
			},
			
			// 再次发起pk
			comeAgain(){
				let that = this
				that.$api.pkIndexCreate().then(res => {
					console.log(res.data.data.active_id,'再来一次')
					uni.redirectTo({
						url: '/pages/pk/pk?active_id=' + res.data.data.active_id
					})
				})
			}
			
		}
	}
</script>

<style lang="less">
	.imagePage{
		.recommend{
			width: 100vw;
			height: 100vh;
			image{
				width: 100%;
				height: 100%;
			}
			.codeTwo{
				width: 300rpx;
				height: 300rpx;
				position: absolute;
				top: 51%;
				left: 30%;
			}
			.btn{
				width: 80%;
				height: 96rpx;
				line-height: 96rpx;
				text-align: center;
				border-radius: 96rpx;
				font-size: 28rpx;
				font-weight: 500;
				position: fixed;
			}
			.invitation{
				background: #000;
				color: #fff;
				bottom: 200rpx;
				left: 50%;
				transform: translateX(-50%);
			}
			.generate{
				background: #DCDCDC;
				color: #7F8084;
				bottom: 70rpx;
				left: 50%;
				transform: translateX(-50%);
			}
		}
		
		.PKWin,.PKNegative{
			width: 100vw;
			height: 100vh;
			image{
				width: 100%;
				height: 100%;
			}
			.btn{
				width: 80%;
				height: 96rpx;
				line-height: 96rpx;
				text-align: center;
				border-radius: 96rpx;
				font-size: 28rpx;
				font-weight: 500;
				position: fixed;
				background: #000;
				color: #fff;
				bottom: 240rpx;
				left: 50%;
				transform: translateX(-50%);
			}
		}

	}
</style>
